package com.alan.validate.config;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties;
import org.apache.log4j.Logger;

public class ErrMessageConfig {
	
	static Logger LOG = Logger.getLogger(ErrMessageConfig.class);
	
	private static Properties prop = null;
	
	public synchronized static void initDate() throws IOException{
		/*if(prop != null){
			return;
		}*/
		prop = new Properties();
		InputStream inStream = ErrMessageConfig.class.getClassLoader().getResourceAsStream("valid-error-message.properties");
		if(inStream == null){
			//ok
			//inStream = ErrMessageConfig.class.getResourceAsStream( "valid-error-default.properties");
			//ok
			inStream = ErrMessageConfig.class.getClassLoader().getResourceAsStream( "valid-error-default.properties");
			//ok
			/*URL fileURL=ErrMessageConfig.class.getResource("/com/alan/validate/config/valid-error-default.properties");     
		    System.out.println(fileURL.getFile());
		    LOG.debug("fileURL="+fileURL.getFile());
			inStream = fileURL.openStream();*/
		}
		InputStreamReader inStreamR = new InputStreamReader(inStream,"UTF-8");
		prop.load(inStreamR);  
		inStream.close();
	}
	
	public static String getProperties(String key) {
		try {
			initDate();
		} catch (IOException e) {
			LOG.error( "初始化验证器错误配置文件异常：",e );
			return null;
		}
		return prop.getProperty( key );
	}
	
	public static void main(String[] args) throws IOException {
		
		InputStream inStream = ErrMessageConfig.class.getResourceAsStream("valid-error-default.properties");
		prop = new Properties();
		prop.load(inStream);  
		inStream.close();
		String tt = prop.getProperty( "com.bbb.Valids.constraints.Email" );
		System.out.println( "tt="+tt );
		/*String tt = getProperties("com.bbb.Valids.constraints.Email");
		System.err.println( "name="+tt );*/
		/*System.out.println(  ErrMessageConfig.class.getName());*/
		
	}
	
}
